package com.lktx.sso.admin.mapper;

import cn.dev33.satoken.stp.StpUtil;
import cn.hserver.core.ioc.IocUtil;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.plugin.mybatis.flex.annotation.Mybatis;
import com.lktx.sso.admin.entity.SsoOrganization;
import com.lktx.sso.admin.entity.SsoUser;
import com.lktx.sso.admin.entity.SsoUserGroupMapping;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * SSO用户表 Mapper 接口
 */
@Mybatis
public interface SsoUserMapper extends BaseMapper<SsoUser> {

    default Map<String, Object> getLoginUserInfo(String id) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        SsoUser ssoUser = selectOneWithRelationsById(id);
        if (ssoUser != null) {
            map.put("sub", ssoUser.getSsoUserId());
            map.put("userId", ssoUser.getSsoUserId());
            if (ssoUser.getAvatar() != null) {
                map.put("avatar", ssoUser.getAvatar());
            }
            if (ssoUser.getNickname() != null) {
                map.put("nickname", ssoUser.getNickname());
            }
            if (ssoUser.getEmail() != null) {
                map.put("email", ssoUser.getEmail());
            }
            if (ssoUser.getPhone() != null) {
                map.put("phone", ssoUser.getPhone());
            }
            if (ssoUser.getName() != null) {
                map.put("name", ssoUser.getName());
            }
            if (ssoUser.getUsername() != null) {
                map.put("username", ssoUser.getUsername());
            }
            SsoOrganization ssoOrganization = ssoUser.getSsoOrganization();
            if (ssoOrganization != null && ssoOrganization.getName() != null) {
                map.put("organizationId", ssoOrganization.getSsoOrganizationId());
                map.put("organizationName", ssoOrganization.getName());
            }
            SsoUserGroupMappingMapper ssoUserGroupMappingMapper = IocUtil.getSupperBean(SsoUserGroupMappingMapper.class);
            if (ssoUserGroupMappingMapper != null) {
                List<SsoUserGroupMapping> ssoUserGroupMappings = ssoUserGroupMappingMapper.selectListWithRelationsByQuery(new QueryWrapper().eq(SsoUserGroupMapping::getSsoUserId, id));
                if (ssoUserGroupMappings != null && !ssoUserGroupMappings.isEmpty()) {
                    List<Map<String, Object>> list = ssoUserGroupMappings.stream().map(k -> {
                        Map<String, Object> data = new LinkedHashMap<>();
                        data.put("id", k.getSsoUserGroupId());
                        if (k.getSsoUserGroup() != null) {
                            data.put("name", k.getSsoUserGroup().getName());
                        }
                        return data;
                    }).toList();
                    map.put("userGroups", list);
                }
            }
        }
        return map;
    }
}
